package com.module.base.utils.imagepicker;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;

import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import com.module.base.glide.SelectorGlideEngine;
import com.module.base.utils.CommonUtils;
import com.luck.picture.lib.PictureSelector;
import com.luck.picture.lib.animators.AnimationType;
import com.luck.picture.lib.config.PictureConfig;
import com.luck.picture.lib.config.PictureMimeType;
import com.luck.picture.lib.entity.LocalMedia;
import com.luck.picture.lib.listener.OnResultCallbackListener;
import com.module.base.R;
import com.yalantis.ucrop.UCrop;
import com.yalantis.ucrop.UCropActivity;
import com.yalantis.ucrop.view.OverlayView;

import java.util.List;

/**
 * forResult 问题
 * 1、OnResultCallbackListener回调方便，但是在部分机型容易出现activity回收，别的框架也一样问题，原理是activity创建fragment 来接受回调
 * 2、ActivityResultLauncher 谷歌的回调必须在onCreate()注册，感觉也鸡肋
 * 3、requestCode  原始方法，，感觉还是只能用这个,一定要在onActivityResult 调用
 */
public class CommonImagePickerUtils {

    public static final int request_code = 963963;
    private static OnResultCallbackListener<LocalMedia> callbackListener;

    public static void showSingleCircleImage(Activity activity) {
        PictureSelector.create(activity)
                .openGallery(PictureMimeType.ofImage())
                .imageEngine(SelectorGlideEngine.createGlideEngine())
                .isWeChatStyle(true)
                .isEnableCrop(true)//裁剪
                .cutOutQuality(30)
                .freeStyleCropMode(OverlayView.DEFAULT_FREESTYLE_CROP_MODE)// 裁剪框拖动模式
                .isCropDragSmoothToCenter(true)// 裁剪框拖动时图片自动跟随居中
                .circleDimmedLayer(true)// 是否开启圆形裁剪
                .withAspectRatio(1, 1)//裁剪比例
                .setCropDimmedColor(CommonUtils.getResource().getColor(R.color.black_80))//设置圆形裁剪背景色值
                .showCropFrame(false)//是否显示裁剪矩形边框 圆形裁剪时建议设为false
                .showCropGrid(false)//是否显示裁剪矩形网格 圆形裁剪时建议设为false
                .selectionMode(PictureConfig.SINGLE)//单图
                .isCamera(true)//列表是否显示拍照按钮
                .isGif(true)//显示gif
                .isPreviewImage(true)//预览
                .isPageStrategy(true, true)//过滤损坏
                .isPreviewVideo(true)//是否预览视频
                .setRecyclerAnimationMode(AnimationType.ALPHA_IN_ANIMATION)
                .forResult(request_code);
    }

    public static void showSingleSquareImage(Activity activity) {
        PictureSelector.create(activity)
                .openGallery(PictureMimeType.ofImage())
                .imageEngine(SelectorGlideEngine.createGlideEngine())
                .isWeChatStyle(true)
                .isEnableCrop(true)//裁剪
                .freeStyleCropMode(OverlayView.DEFAULT_FREESTYLE_CROP_MODE)// 裁剪框拖动模式
                .isCropDragSmoothToCenter(true)// 裁剪框拖动时图片自动跟随居中
                .withAspectRatio(1, 1)//裁剪比例
                .setCropDimmedColor(CommonUtils.getResource().getColor(R.color.black_80))//设置圆形裁剪背景色值
                .showCropFrame(false)//是否显示裁剪矩形边框 圆形裁剪时建议设为false
                .showCropGrid(false)//是否显示裁剪矩形网格 圆形裁剪时建议设为false
                .selectionMode(PictureConfig.SINGLE)//单图
                .isCamera(true)//列表是否显示拍照按钮
                .isGif(true)//显示gif
                .isPreviewImage(true)//预览
                .isPageStrategy(true, true)//过滤损坏
                .isPreviewVideo(true)//是否预览视频
                .setRecyclerAnimationMode(AnimationType.ALPHA_IN_ANIMATION)
                .forResult(request_code);
    }


    public static void showSingleImage(Activity activity) {
        PictureSelector.create(activity)
                .openGallery(PictureMimeType.ofImage())
                .imageEngine(SelectorGlideEngine.createGlideEngine())
                .isWeChatStyle(true)
                .selectionMode(PictureConfig.SINGLE)//单图
                .isCamera(true)//列表是否显示拍照按钮
                .isGif(true)//显示gif
                .isPreviewImage(true)//预览
                .isPageStrategy(true, true)//过滤损坏
                .isPreviewVideo(true)//是否预览视频
                .setRecyclerAnimationMode(AnimationType.ALPHA_IN_ANIMATION)
                .forResult(request_code);
    }

    public static void showSingleImage(Fragment fragment) {
        PictureSelector.create(fragment)
                .openGallery(PictureMimeType.ofImage())
                .imageEngine(SelectorGlideEngine.createGlideEngine())
                .isWeChatStyle(true)
                .selectionMode(PictureConfig.SINGLE)//单图
                .isCamera(true)//列表是否显示拍照按钮
                .isGif(true)//显示gif
                .isPreviewImage(true)//预览
                .isPageStrategy(true, true)//过滤损坏
                .isPreviewVideo(true)//是否预览视频
                .setRecyclerAnimationMode(AnimationType.ALPHA_IN_ANIMATION)
                .forResult(request_code);
    }

    public static void showSingle(Activity activity, int chooseMode) {
        PictureSelector.create(activity)
                .openGallery(chooseMode)
                .imageEngine(SelectorGlideEngine.createGlideEngine())
                .isWeChatStyle(true)
                .selectionMode(PictureConfig.SINGLE)//单图
                .isCamera(true)//列表是否显示拍照按钮
                .isGif(true)//显示gif
                .isPreviewImage(true)//预览
                .isPageStrategy(true, true)//过滤损坏
                .isPreviewVideo(true)//是否预览视频
                .setRecyclerAnimationMode(AnimationType.ALPHA_IN_ANIMATION)
                .forResult(request_code);
    }

    public static void showSingle(Fragment fragment, int chooseMode) {
        PictureSelector.create(fragment)
                .openGallery(chooseMode)
                .imageEngine(SelectorGlideEngine.createGlideEngine())
                .isWeChatStyle(true)
                .selectionMode(PictureConfig.SINGLE)//单图
                .isCamera(true)//列表是否显示拍照按钮
                .isGif(true)//显示gif
                .isPreviewImage(true)//预览
                .isPageStrategy(true, true)//过滤损坏
                .isPreviewVideo(true)//是否预览视频
                .setRecyclerAnimationMode(AnimationType.ALPHA_IN_ANIMATION)
                .forResult(request_code);
    }

    public static void show(Activity activity, int chooseMode, int maxNum) {
        PictureSelector.create(activity)
                .openGallery(chooseMode)
                .imageEngine(SelectorGlideEngine.createGlideEngine())
                .isWeChatStyle(true)
                .maxSelectNum(maxNum)
                .maxVideoSelectNum(maxNum)
                .isCamera(true)//列表是否显示拍照按钮
                .isGif(true)//显示gif
                .isPageStrategy(true, true)//过滤损坏
                .isPreviewImage(true)//预览
                .isPreviewVideo(true)//是否预览视频
                .setRecyclerAnimationMode(AnimationType.ALPHA_IN_ANIMATION)
                .forResult(request_code);
    }

    public static void show(Activity activity, int chooseMode) {
        PictureSelector.create(activity)
                .openGallery(chooseMode)
                .imageEngine(SelectorGlideEngine.createGlideEngine())
                .isWeChatStyle(true)
                .selectionMode(PictureConfig.MULTIPLE)//多图
                .maxSelectNum(100000)
                .maxVideoSelectNum(100000)
                .isPageStrategy(true, true)//过滤损坏
                .isCamera(true)//列表是否显示拍照按钮
                .isGif(true)//显示gif
                .isPreviewImage(true)//预览
                .isPreviewVideo(true)//是否预览视频
                .setRecyclerAnimationMode(AnimationType.ALPHA_IN_ANIMATION)
                .forResult(request_code);
    }

    public static void show(Fragment fragment, int chooseMode) {
        PictureSelector.create(fragment)
                .openGallery(chooseMode)
                .imageEngine(SelectorGlideEngine.createGlideEngine())
                .isWeChatStyle(true)
                .selectionMode(PictureConfig.MULTIPLE)//多图
                .maxSelectNum(100000)
                .isPageStrategy(true, true)//过滤损坏
                .isCamera(true)//列表是否显示拍照按钮
                .isGif(true)//显示gif
                .isPreviewImage(true)//预览
                .isPreviewVideo(true)//是否预览视频
                .setRecyclerAnimationMode(AnimationType.ALPHA_IN_ANIMATION)
                .forResult(request_code);
    }


    public static void showRecord(Activity activity, int chooseMode, int maxNum, boolean isWithVideoImage, long filterMaxFileSize) {
        PictureSelector.create(activity)
                .openGallery(chooseMode)
                .imageEngine(SelectorGlideEngine.createGlideEngine())
                .isWeChatStyle(true)
                .maxSelectNum(maxNum)
                .maxVideoSelectNum(isWithVideoImage ? maxNum : 1)
                .isWithVideoImage(isWithVideoImage)
                .isPageStrategy(true, true)//过滤损坏
                .filterMaxFileSize(filterMaxFileSize)//过滤50M以上
                .isCamera(true)//列表是否显示拍照按钮
                .isGif(true)//显示gif
                .isPreviewImage(true)//预览
                .isPreviewVideo(true)//是否预览视频
                .setRecyclerAnimationMode(AnimationType.ALPHA_IN_ANIMATION)
                .forResult(request_code);
    }


    /**
     * 回调后，不涉及Activity 界面调用才可以使用，例如static 方法里面调用，
     * 如：ConvertGifUtils
     * <p>
     * 可直接回调
     */
    @Deprecated
    public static void showSingle(Activity activity, int chooseMode, OnResultCallbackListener<LocalMedia> listener) {
        callbackListener = listener;
        PictureSelector.create(activity)
                .openGallery(chooseMode)
                .imageEngine(SelectorGlideEngine.createGlideEngine())
                .isWeChatStyle(true)
                .selectionMode(PictureConfig.SINGLE)//单图
                .isCamera(true)//列表是否显示拍照按钮
                .isGif(true)//显示gif
                .isPreviewImage(true)//预览
                .isPreviewVideo(true)//是否预览视频
                .isPageStrategy(true, true)//过滤损坏
                .setRecyclerAnimationMode(AnimationType.ALPHA_IN_ANIMATION)
                .forResult(listener);
    }

    /**
     * 回调后，不涉及Activity 界面调用才可以使用，例如static 方法里面调用，
     * 如：ConvertGifUtils
     * <p>
     * 可直接回调
     */
    @Deprecated
    public static void showCrop(Activity activity, int chooseMode, OnResultCallbackListener<LocalMedia> listener) {
        callbackListener = listener;
        UCrop.Options options = new UCrop.Options();
        options.setCompressionFormat(Bitmap.CompressFormat.PNG);
        //使用PNG的话，setCompressionQuality则失效，靠withMaxResultSize来控制大小
        options.setCompressionQuality(100);
        options.setAllowedGestures(UCropActivity.ALL, UCropActivity.ALL, UCropActivity.ALL);

        PictureSelector.create(activity)
                .openGallery(chooseMode)
                .imageEngine(SelectorGlideEngine.createGlideEngine())
                .isWeChatStyle(true)
                .selectionMode(PictureConfig.SINGLE)//单图
                .isCamera(true)//列表是否显示拍照按钮
                .isEnableCrop(true)
                .basicUCropConfig(options)
                .isGif(true)//显示gif
                .isEnableCrop(true)
                .isPreviewImage(true)//预览
                .isPreviewVideo(true)//是否预览视频
                .isPageStrategy(true, true)//过滤损坏
                .setRecyclerAnimationMode(AnimationType.ALPHA_IN_ANIMATION)
                .forResult(listener);
    }

    public static boolean onActivityResult(int requestCode, int resultCode) {
        if (request_code == requestCode && resultCode == Activity.RESULT_OK) {
            return true;
        }
        return false;
    }

    public static List<LocalMedia> getList(@Nullable Intent data) {
        List<LocalMedia> selectList = PictureSelector.obtainMultipleResult(data);
        return selectList;
    }
}
